package com.example.roleplay.controller;

import com.example.roleplay.domain.Persona;
import com.example.roleplay.service.PersonaService;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理API控制器
 */
@RestController
@RequestMapping("/api/personas")
@RequiredArgsConstructor

public class PersonaController {

    private final PersonaService personaService;

    @GetMapping("/all")

    public ResponseEntity<List<Persona>> getAllPersonas() {
        List<Persona> personas = personaService.getAllPersonas();
        return ResponseEntity.ok(personas);
    }

    @GetMapping("/{personaId}")

    public ResponseEntity<Persona> getPersonaById(
            @PathVariable String personaId) {
        Persona persona = personaService.getPersonaById(personaId);
        if (persona != null) {
            return ResponseEntity.ok(persona);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/initials")

    public ResponseEntity<Map<String, String>> getPersonaInitialsMapping() {
        Map<String, String> mapping = new HashMap<>();
        mapping.put("SG", "苏格拉底");
        mapping.put("SW", "孙悟空");
        mapping.put("DF", "列奥纳多·达·芬奇");
        return ResponseEntity.ok(mapping);
    }

    @GetMapping("/by-initials/{initials}")

    public ResponseEntity<Persona> getPersonaByInitials(
            @PathVariable String initials) {
        
        // 将缩写映射到角色ID
        String personaId = switch (initials.toUpperCase()) {
            case "SG" -> "SG";  // 苏格拉底
            case "SW" -> "SW";  // 孙悟空
            case "DF" -> "DF";  // 达芬奇
            default -> null;
        };
        
        if (personaId == null) {
            return ResponseEntity.badRequest().build();
        }
        
        Persona persona = personaService.getPersonaById(personaId);
        if (persona != null) {
            return ResponseEntity.ok(persona);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/search")

    public ResponseEntity<List<Persona>> searchPersonas(
            @RequestParam(required = false) String keyword) {
        List<Persona> personas = personaService.searchPersonas(keyword);
        return ResponseEntity.ok(personas);
    }
}